package DoExercise.JianzhiOffer;


/**
 * 删除链表的节点
 * <p>
 * https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
 */
public class Offer081_删除链表的节点
{
    
    
    public static void main(String[] args)
    {
        Offer000_Common.ListNode l1 = new Offer000_Common.ListNode(1);
        Offer000_Common.ListNode l2 = new Offer000_Common.ListNode(2);
        Offer000_Common.ListNode l3 = new Offer000_Common.ListNode(3);
        Offer000_Common.ListNode l4 = new Offer000_Common.ListNode(4);
        
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        Offer000_Common.ListNode node = deleteNode(l1, 3);
        while (node != null)
        {
            System.out.println(node.val);
            node = node.next;
        }
    }
    
    public static Offer000_Common.ListNode deleteNode(Offer000_Common.ListNode head, int val)
    {
        if (head.val == val) return head.next;
        Offer000_Common.ListNode pre = null, cur = head;
        while (cur != null && cur.val != val)
        {
            pre = cur;
            cur = cur.next;
        }
        if (cur != null) pre.next = cur.next;
        return head;
    }
}
